package delegate4j;

import com.sun.istack.internal.NotNull;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;

@FunctionalInterface
public interface ICallback extends IDelegate{
    ExecutorService ASYNC_CALLBACK_THREAD_POOL = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2);

    void invoke(@NotNull InvokeData data) throws Exception ;

    default FutureTask<Void> invokeAsync(@NotNull InvokeData data){
        FutureTask<Void> futureTask = new FutureTask<>(() -> {
            try {
                invoke(data);
            }catch (Exception ignore){

            }
            return null;
        });
        ASYNC_CALLBACK_THREAD_POOL.submit(futureTask);
        return futureTask;
    }
}
